﻿using System;
using Tanis.RouteService;

namespace Tanis.WebService
{
    public class WebServiceManager : IDisposable
    {    	
        #region Contructor & destructor
		public WebServiceManager()
		{
            _client = new APIClient("WSHttpBinding_IAPI"); // also possible to use "BasicHttpBinding_IAPI" //
			_client.FindRoutesCompleted += OnSearchCompleted;
			_client.GetAvailableMobileScopesCompleted += OnCityListLoaded;			
			_client.GetStopNamesCompleted += OnGetStopNamesCompleted;
		}

        public void Dispose()
        {
            if (_client != null)
            {
                _client.FindRoutesCompleted -= OnSearchCompleted;
                _client.GetStopNamesCompleted -= OnGetStopNamesCompleted;
                _client.GetAvailableMobileScopesCompleted -= OnCityListLoaded;
            }
        }
        #endregion

        #region Private members and properties
        readonly APIClient _client;
        #endregion

        #region Event declarations
        public event EventHandler<FindRoutesCompletedEventArgs> SearchPerformed;
		public event EventHandler<GetStopNamesCompletedEventArgs> StopListLoaded;
        public event EventHandler<GetAvailableMobileScopesCompletedEventArgs> CityListLoaded;
        #endregion

        #region Process received results from web service (private)
        private void OnCityListLoaded(object sender, GetAvailableMobileScopesCompletedEventArgs args)
    	{
            CityListLoaded(sender, args);
    	}

		private void OnGetStopNamesCompleted(object sender, GetStopNamesCompletedEventArgs args)
		{
            StopListLoaded(sender, args);
		}

    	private void OnSearchCompleted(object sender, FindRoutesCompletedEventArgs args)
    	{
            SearchPerformed(sender, args);
    	}
		#endregion

		#region Public methods for data retrieval
		/// <summary>
    	/// Get route from point A to point B
    	/// </summary>
    	/// <param name="scope">City or location name</param>
    	/// <param name="startPlace">Departure station name or address</param>
    	/// <param name="endPlace">Destination station name or address</param>
    	/// <param name="time">Time and date for search</param>
    	/// <param name="isArrival">Sets if time specified is for departure or arrival</param>
    	public void GetRoutes(string scope, string startPlace, string endPlace, DateTime time, bool isArrival)
    	{
            _client.FindRoutesAsync(scope, startPlace, endPlace, time, isArrival, string.Empty, string.Empty, -1, 0, 0, 0, false, string.Empty, string.Empty);
    	}

		/// <summary>
		/// Get stops in given city
		/// </summary>
        /// <param name="city">City or location name</param>
        public void GetStopList(string city)
		{
            if (string.IsNullOrEmpty(city))
            {
                return;
            }

			_client.GetStopNamesAsync(city);
		}

    	/// <summary>
		/// Get stops in given city
		/// </summary>
		public void GetCityList()
    	{
    		_client.GetAvailableMobileScopesAsync();
    	}
		#endregion
    }
}
